package com.springcloud.auth.utils;


import com.springcloud.auth.config.OauthConfig;
import com.springcloud.auth.entity.TUserInfo;
import com.springcloud.auth.service.TUserInfoService;
import com.springcloud.commom.utils.SpringContextUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.MediaType;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * token工具 2019-4-10 hzl
 */

public class TokenUtil {

	private static final String USER_ID = "userId";
	private static final String USER_USERNAME = "userName";
	private static final String USER_PHONE = "phone";
	private static final String LOGIN_NAME = "loginName";
	private static final String CREATE_TIME = "createTime";

	/**
	 * 存放鉴权信息的Header名称，默认是Authorization
	 */

	private static Log log = LogFactory.getLog(TokenUtil.class);

	/**
	 * 保存当前的用户
	 */
	private static final ThreadLocal<TUserInfo> currentUser = new ThreadLocal<>();

	/**
	 * 保存
	 */
	private static final ThreadLocal<Map<String, String>> globalMap = new ThreadLocal<>();

	/**
	 * 获得一个token
	 * 
	 * @param
	 * @return
	 */
	public static String getToken(Map<String, String> map) {
		return TokenHelper.generateToken(map);
	}

	public static void setUserInfo(Map<String, String> map) {
		String userId = map.get(OauthConfig.TOKEN_USER_ID);
		TUserInfoService sevice = SpringContextUtils.getBean(TUserInfoService.class);
		TUserInfo userInfo = sevice.getById(userId);
		
		currentUser.set(userInfo);
	}

	public static void setGlobalMap(Map<String, String> map) {
		globalMap.set(map);
	}
	
	public static Map<String, String> getGlobalMap() {
		return globalMap.get();
	}

	public static String getUserId(Map<String, String> map) {
		return map.get(OauthConfig.TOKEN_USER_ID);
	}
	
	public static String getAcToken(Map<String, String> map) {
		return map.get(OauthConfig.TOKEN_ACCESS_TOKEN);
	}
	
	public static String getReToken(Map<String, String> map) {
		return map.get(OauthConfig.TOKEN_REFRESH_TOKEN);
	}

	public static TUserInfo getUserInfo() {
		return currentUser.get();
	}

	public static void write(HttpServletResponse response, Object object) {
		response.setContentType(MediaType.APPLICATION_JSON_VALUE);
		PrintWriter out = null;
		try {
			out = response.getWriter();
			out.println(object);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != out) {
				out.flush();
				out.close();
			}
		}
	}

	/**
	 * 创建token
	 * @param userId
	 * @param acToken
	 * @param reToken
	 * @return
	 */
	public static String createToken(String userId, String acToken, String reToken) {
		Map<String, String> tokenMap = new HashMap<>();
		tokenMap.put(OauthConfig.TOKEN_ACCESS_TOKEN, acToken);
		tokenMap.put(OauthConfig.TOKEN_REFRESH_TOKEN, reToken);
		tokenMap.put(OauthConfig.TOKEN_USER_ID, userId);

		String token = TokenUtil.getToken(tokenMap);
		return token;
	}

	public static String getUserId() {
		TUserInfo userInfo = TokenUtil.getUserInfo();
		return userInfo != null ? userInfo.getId() : "";
	}
}
